#!/usr/bin/env python
#coding=utf-8

import wx
import wx.lib.pubsub

class CategoryInteractor(object):
    ''' Interactions between view and presentation. '''

    def Install(self, presenter, view):
        self.presenter = presenter
        self.view = view
        view.categoryLst.Bind(wx.EVT_LISTBOX, self.OnReloadNeeded)
        view.categoryInput.Bind(wx.EVT_TEXT, self.OnDataFieldUpdated)
        view.cancelBtn.Bind(wx.EVT_BUTTON, self.OnReloadNeeded)
        view.submitBtn.Bind(wx.EVT_BUTTON, self.OnSave)
        view.Bind(wx.EVT_CLOSE, self.OnClose)
        view.addBtn.Bind(wx.EVT_BUTTON, self.OnAddNewCategory)
        view.orderBtn.Bind(wx.EVT_BUTTON, self.OnSort)

    def OnAddNewCategory(self, evt):
        self.presenter.add_new_category()

    def OnSort(self, event):
        self.presenter.change_sort_order()

    def OnClose(self,event):
        ''' Catch close window events.

            Unfortunately, the listbox also raises an update with a list
            idx of -1. This idx leads to a memory leak.
         '''
        self.presenter.isListening=False
        self.view.Destroy()

    def OnDataFieldUpdated(self, evt):
        self.presenter.dataFieldUpdated()

    def OnReloadNeeded(self, evt):
        self.presenter.loadViewFromModel()

    def OnSave(self,evt):
        ''' Let the presenter update the database. '''

        self.presenter.updateModel()
        wx.lib.pubsub.Publisher().sendMessage('Category')

class PreferencesInteractor(object):
    ''' Take care of signals issued by the Preferences Window. '''

    def Install(self, presenter, view):
        ''' Create the widget bindings. '''
        self.presenter = presenter
        self.view = view

        view.cancelBtn.Bind(wx.EVT_BUTTON, self.OnCancel)
        view.saveBtn.Bind(wx.EVT_BUTTON, self.OnSave)

        view.voiceOutputCtrl.Bind(wx.EVT_COMBOBOX, self.OnNewSpeaker)

    def OnCancel(self,event):
        ''' Cancel editing preferences. '''

        self.presenter.quit()

    def OnNewSpeaker(self, event):
        self.presenter.set_voice_prg_path()

    def OnSave(self, event):
        ''' Update configuration '''

        self.presenter.save()

class MainInteractor(object):
    ''' Translate wx events into the "higher level language" of the presenter.
    '''

    def Install(self, presenter, view):
        self.presenter = presenter
        self.view = view

        view.Bind(wx.EVT_MENU, self.OnAbout, id = view.aboutItem.GetId())
        view.Bind(wx.EVT_MENU, self.OnCategory, id = view.categoryItem.GetId())
        view.Bind(wx.EVT_MENU, self.OnExport, id = view.exportItem .GetId())
        view.Bind(wx.EVT_MENU, self.OnPreferences,
                                            id = view.preferencesItem.GetId())
        view.Bind(wx.EVT_MENU, self.OnQuit, id = view.quitItem.GetId())

        # inputPanel
        view.notebook.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnTabChange)
        view.notebook.inputPanel.submitBtn.Bind(
            wx.EVT_BUTTON, self.OnSaveNewVoc)
        view.notebook.inputPanel.cancelBtn.Bind(
            wx.EVT_BUTTON, self.OnCancelInput)
        # learnPanel
        view.notebook.learnPanel.startBtn.Bind(
                                    wx.EVT_BUTTON, self.OnStartBtn)
        view.notebook.learnPanel.verifyBtn.Bind(
                                    wx.EVT_BUTTON, self.OnVerifyBtn)
        view.notebook.learnPanel.answerInput.Bind(
                                    wx.EVT_TEXT_ENTER, self.OnVerifyBtn)
        view.notebook.learnPanel.loudspeaker.Bind(
                                    wx.EVT_BUTTON, self.OnSpeak)
        # editPanel

        view.notebook.editPanel.searchInput.Bind(
            wx.EVT_TEXT_ENTER, self.OnSearchBtn)
        view.notebook.editPanel.printBtn.Bind(
            wx.EVT_BUTTON, self.OnPrintBtn)
        view.notebook.editPanel.clearBtn.Bind(
            wx.EVT_BUTTON, self.OnClearBtn)
        # The double click event has no listCtrl access. Therefore, we
        # are remembering the id on the first 'normal' click.
        view.notebook.editPanel.vocLst.Bind(
            wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
        view.notebook.editPanel.vocLst.Bind(
            wx.EVT_LEFT_DCLICK, self.OnEdit)

        view.notebook.editPanel.vocLst.Bind(
            wx.EVT_CONTEXT_MENU, self.OnContextMenu)

        view.notebook.editPanel.vocLst.Bind(
            wx.EVT_MENU, self.OnEditMenuEvt)

        wx.lib.pubsub.Publisher().subscribe(
            self.presenter.refresh_categories, topic = 'Category')

    def OnAbout(self, event):
        ''' Present the About Dialog. '''

        self.presenter.show_about_box()

    def OnCancelInput(self, event):
        ''' Reset all input fields, if the cancel button was pressed. '''

        self.presenter.refresh_input_tab()

    def OnCategory(self,event):
        ''' React on Options/Category. '''

        self.presenter.show_category_window()

    def OnClearBtn(self, event):
        '''Delete searchstring and view list. '''

        self.presenter.clear_search()

    def OnContextMenu(self, event):
         menu = wx.Menu(title="Delete")
         self.presenter.show_context_menu()

    def OnEdit(self, event):
        ''' Edit a vocabulary. '''

        self.presenter.update_input_panel_wiht_voc()

    def OnEditMenuEvt(self, event):
        '''Get event from right click in edit panel.

        Asks the user to delete the voc.
        '''
        if event.GetId() == wx.ID_CUT:

        #wx.MessageBox(str(dir(event)))
        #wx.MessageBox(str(event.GetId()))
            self.presenter.ask_user()

    def OnExport(self, event):
        ''' Export the vocs as csv file. '''

        self.presenter.export_vocs()

    def OnSpeak(self, event):
        '''Speak out loudley the vocabulary text.'''
        self.presenter.speak()


    def OnTabChange(self, event):
        ''' '''

        label = self.presenter.get_notebook_page_label()
        #label = self.view.notebook.GetPageText(self.view.notebook.GetSelection())
        if label == 'Input':
            self.presenter.refresh_input_tab()
        elif label == 'Edit':
            self.presenter.set_focus_in_edit_panel()

    def OnItemSelected(self, event):
        ''' '''
        self.presenter.set_selected_item(event.m_itemIndex)

    def OnPreferences(self, event):
        ''' Open new frame for editing preferences. '''

        self.presenter.show_preferences_window()

    def OnPrintBtn(self, event):
        ''' Open preview for printing selected vocs. '''

        self.presenter.print_vocs()

    def OnQuit(self, event):
        ''' Close the application. '''

        self.presenter.quit()

    def OnSaveNewVoc(self, event):
        ''' Call presentation for saving new voc. '''

        self.presenter.save_or_update_voc()

    def OnSearchBtn(self, event):
        ''' User want to fitler the vocabulary list on the edit panel. '''

        self.presenter.refresh_voc_list(search=True)

    def OnStartBtn(self, event):
        ''' User want to learn vocabularies. '''

        self.presenter.toggle_learning()

    def OnVerifyBtn(self, event):
        ''' Verify the translation entered by the user '''

        self.presenter.verify_voc()